"""
数据库模型定义
包含了所有数据库表的结构
"""
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone


class User(AbstractUser):
    """
    用户表，继承Django自带的用户模型
    添加了角色、头像和个人简介字段
    """
    ROLE_CHOICES = (
        ('reader', '读者'),
        ('author', '作者'),
        ('admin', '管理员'),
    )
    role = models.CharField(max_length=10, choices=ROLE_CHOICES, default='reader', verbose_name='用户角色')
    avatar = models.ImageField(upload_to='avatars/', null=True, blank=True, verbose_name='头像')
    bio = models.TextField(max_length=500, blank=True, verbose_name='个人简介')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.username


class Category(models.Model):
    """
    小说分类表
    存储小说的分类信息
    """
    name = models.CharField(max_length=50, unique=True, verbose_name='分类名称')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '小说分类'
        verbose_name_plural = '小说分类'

    def __str__(self):
        return self.name


class Novel(models.Model):
    """
    小说表
    存储小说的基本信息
    """
    STATUS_CHOICES = (
        ('pending', '审核中'),
        ('approved', '已通过'),
        ('rejected', '未通过'),
    )
    title = models.CharField(max_length=100, verbose_name='小说标题')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='novels', verbose_name='作者')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='novels', verbose_name='分类')
    cover = models.ImageField(upload_to='covers/', null=True, blank=True, verbose_name='封面')
    description = models.TextField(verbose_name='小说简介')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '小说'
        verbose_name_plural = '小说'

    def __str__(self):
        return self.title


class Chapter(models.Model):
    """
    小说章节表
    存储小说的章节内容
    """
    novel = models.ForeignKey(Novel, on_delete=models.CASCADE, related_name='chapters', verbose_name='所属小说')
    title = models.CharField(max_length=100, verbose_name='章节标题')
    content = models.TextField(verbose_name='章节内容')
    order = models.IntegerField(default=0, verbose_name='章节顺序')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '小说章节'
        verbose_name_plural = '小说章节'
        ordering = ['order']  # 按章节顺序排序

    def __str__(self):
        return f"{self.novel.title} - {self.title}"


class Bookshelf(models.Model):
    """
    用户书架表
    记录用户收藏的小说和阅读进度
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookshelf', verbose_name='用户')
    novel = models.ForeignKey(Novel, on_delete=models.CASCADE, related_name='in_bookshelves', verbose_name='小说')
    last_read_chapter = models.ForeignKey(Chapter, on_delete=models.SET_NULL, null=True, blank=True,
                                          verbose_name='最后阅读章节')
    added_at = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '用户书架'
        verbose_name_plural = '用户书架'
        unique_together = ['user', 'novel']  # 确保用户不会重复添加同一本小说

    def __str__(self):
        return f"{self.user.username} - {self.novel.title}"


class AuthorApplication(models.Model):
    """
    作者申请表
    记录用户申请成为作者的信息
    """
    STATUS_CHOICES = (
        ('pending', '审核中'),
        ('approved', '已通过'),
        ('rejected', '未通过'),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author_applications', verbose_name='申请用户')
    reason = models.TextField(verbose_name='申请理由')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='申请时间')
    processed_at = models.DateTimeField(null=True, blank=True, verbose_name='处理时间')
    admin_notes = models.TextField(blank=True, verbose_name='管理员备注')

    class Meta:
        verbose_name = '作者申请'
        verbose_name_plural = '作者申请'

    def __str__(self):
        return f"{self.user.username} - {self.get_status_display()}"
